package com.miyake.loveviewweb.client.viewer;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.client.chart.IChart;
import com.miyake.loveviewweb.client.chart.LvChartFactory;
import com.miyake.loveviewweb.shared.model.ChartModel;

public class ChartViewer extends Viewer {
    private IChart chart = LvChartFactory.getInstance().createSimpleDynamicChart();
    private ChartModel chartModel;
    
    public ChartViewer(ChartModel chartModel) {
        this.chartModel = chartModel;
    }
    
    private void updateChart(ChartModel chartModel) {
        chart.clearPlot(0);        
        chart.setData(chartModel);
    }


    @Override
    String getPageTitle() {
        return chartModel.getName();
    }

    @Override
    Widget getContent() {
        chart.setChartSize(300, 200);
        final DataServiceWrapper serviceWrapper = DataServiceWrapper.getInstance();           
        ChartModel chartModel = serviceWrapper.getSelectedChart();
        if (chartModel != null) {
            if (chartModel.isContent()) {
                updateChart(chartModel);
            }
            else {
                serviceWrapper.getService().getChart(chartModel.getKey(), true, new AsyncCallback<ChartModel>() {
                    public void onFailure(Throwable caught) {
                    }
    
                    public void onSuccess(ChartModel result) {
                        updateChart(result);
                    }
                    
                });
            }
        }
        return chart.getUiWidget();
    }

    @Override
    Widget getToolbar() {
        return null;
    }
}
